#include using namespace std; const int MATRIX_SIZE = 4; const int MATRIX_COUNT = 25; void getInititalMatrices(int matrices[][MATRIX_SIZE][MATRIX_SIZE], int& matricesUsed) { int initialMatrices; do { cout << "How many initial matrices?"; cin >> initialMatrices; } while(initialMatrices < 1 || initialMatrices > MATRIX_COUNT); while(matricesUsed < initialMatrices) { cout << "Enter matrix " << matricesUsed + 1 << endl; for(int row = 0; row < MATRIX_SIZE; row++) { cout << " Row " << row + 1 << "? "; for(int column = 0; column < MATRIX_SIZE; column++) { cin >> matrices[matricesUsed][row][column]; } } matricesUsed++; cout << endl; } } void getOperation(char& operation, int matricesUsed) { bool validOperation = false; do { cout << "Operation? "; cin >> operation; if( matricesUsed < MATRIX_COUNT) { if(operation == 'Q' || operation == 'q' || operation == 'T' || operation == 't' || operation == '+' || operation == '-' || operation == 'D' || operation == 'd' || operation == 'x' || operation == 'X' || operation == '*' ) { validOperation = true; } else { cout << "Invalid Operation...\n"; } } else { if(operation == 'Q' || operation == 'q' || operation == 'D' || operation == 'd') { validOperation = true; } else { cout << "Invalid Operation, there is not room for more results...\n"; } } } while(! validOperation); } void getMatrixIndex(int& index, int matricesUsed, char prompt[]) { do { cout << prompt; cin >> index; } while(index < 1 || index > matricesUsed); } void displayMatrix(int matrix[][MATRIX_SIZE]) { for(int row = 0; row < MATRIX_SIZE; row++) { cout << " Row " << row + 1 << ": "; for(int column = 0; column < MATRIX_SIZE; column++) { cout << matrix[row][column] << " "; } cout << endl; } } void addMatrices(int matrix1[][MATRIX_SIZE], int matrix2[][MATRIX_SIZE], int result[][MATRIX_SIZE]) { for(int row = 0; row < MATRIX_SIZE; row++) { for(int column = 0; column < MATRIX_SIZE; column++) { result[row][column] = matrix1[row][column] + matrix2[row][column]; } } } void displayMatrix(int matrices[][MATRIX_SIZE][MATRIX_SIZE], int& matricesUsed) { int index; getMatrixIndex(index,matricesUsed, "Matrix to Display?"); cout << "Matrix " << index << endl; displayMatrix(matrices[index-1]); } void main() { int matrices[MATRIX_COUNT][MATRIX_SIZE][MATRIX_SIZE]; int matricesUsed = 0; char operation; getInititalMatrices(matrices, matricesUsed); getOperation(operation, matricesUsed); while(toupper(operation) != 'Q') { int index; int index2; //do the operation switch(toupper(operation)) { case 'D': displayMatrix(matrices,matricesUsed); break; case 'T': getMatrixIndex(index,matricesUsed, "Matrix to Tranpose?"); //transposeMatrix(matrices[index-1], matrices[matricesUsed]); matricesUsed++; cout << "Result is Matrix " << matricesUsed << endl; displayMatrix(matrices[matricesUsed-1]); break; case '+': getMatrixIndex(index,matricesUsed, "First Matrix for +?"); getMatrixIndex(index2,matricesUsed, "Second Matrix for +?"); addMatrices(matrices[index-1], matrices[index2-1], matrices[matricesUsed]); matricesUsed++; cout << "Result is Matrix " << matricesUsed << endl; displayMatrix(matrices[matricesUsed-1]); break; case '-': getMatrixIndex(index,matricesUsed, "First Matrix for -?"); getMatrixIndex(index2,matricesUsed, "Second Matrix for -?"); //subractMatrices(matrices[index-1], matrices[index2-1], matrices[matricesUsed]); matricesUsed++; cout << "Result is Matrix " << matricesUsed << endl; displayMatrix(matrices[matricesUsed-1]); break; case 'X': getMatrixIndex(index,matricesUsed, "First Matrix for X?"); getMatrixIndex(index2,matricesUsed, "Second Matrix for X?"); //multiplyMatrices(matrices[index-1], matrices[index2-1], matrices[matricesUsed]); matricesUsed++; cout << "Result is Matrix " << matricesUsed << endl; displayMatrix(matrices[matricesUsed-1]); break; case '*': int scalar; cout << "Scalar value for *? "; cin >> scalar; getMatrixIndex(index,matricesUsed, "Matrix for *?"); //multiplyMatrixAndScalar(matrices[index-1], scalar, matrices[matricesUsed]); matricesUsed++; cout << "Result is Matrix " << matricesUsed << endl; displayMatrix(matrices[matricesUsed-1]); break; } //display the new matrix getOperation(operation, matricesUsed); } }